package easy

import "sort"

func SortedSquares(nums []int) []int {
	ans := make([]int, len(nums))
	for i, v := range nums {
		ans[i] = v * v
	}
	sort.Ints(ans)
	return ans
}

func SortedSquares1(nums []int) []int {
	n := len(nums)
	ans := make([]int, n)
	left, right, size := 0, n-1, n-1
	for left <= right {
		if nums[left]*nums[left] > nums[right]*nums[right] {
			ans[size] = nums[left] * nums[left]
			left++
		} else {
			ans[size] = nums[right] * nums[right]
			right--
		}
		size--
	}
	return ans
}
